package com.newtouch.bxzs.business.message.websocket.config;

import com.newtouch.bxzs.business.message.websocket.constant.Constant;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * <p>
 *  webSocket配置
 *
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/2/2 15:15</td>
 * <td>tianshangzhu</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author tianshangzhu
 * @version 1.0.0
 * @since 1.0.0
 */
@Configuration
@EnableWebSocket
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
        //注册一个Stomp的节点（endpoint）,并指定使用SockJS协议。 /endpointWisely
        stompEndpointRegistry.addEndpoint(Constant.WEBSOCKET_PATH)
                // 解决跨域问题
                .setAllowedOrigins("*")
                .withSockJS();
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        //服务端发送消息给客户端的域,多个用逗号隔开  /topic
        registry.enableSimpleBroker(Constant.WEBSOCKET_BROADCAST_PATH, Constant.P2P_PUSH_BASEPATH);
        //定义一对一推送的时候前缀  /user
        registry.setUserDestinationPrefix(Constant.P2P_PUSH_BASEPATH);
        //定义webSocket前缀  /ws-push
        registry.setApplicationDestinationPrefixes(Constant.WEBSOCKET_PATH_PERFIX);
    }
}
